package com.dycn.service;

import com.dycn.dao.ActivitysMapper;
import com.dycn.model.Activitys;
import com.dycn.util.AppResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ActivitysService {
    @Autowired
    ActivitysMapper activitysMapper;

    public AppResultData listActivitys(int page,Integer type,HttpServletRequest httpServletRequest){
        PageHelper.startPage(page,10);
        List<Activitys> activitysList=activitysMapper.findByType(type);
        PageInfo pageInfo=new PageInfo(activitysList);
        List<Activitys> activitysList1=pageInfo.getList();
        String url=getUrl(httpServletRequest);
        for (Activitys activitys:activitysList1){
            switch (type){
                case 0:
                    //点赞活动
                    activitys.setUrl(url+"/tasks/list?type="+type+"&activityId="+activitys.getId());
                    break;
                case 1:
                    //投票活动
                    activitys.setUrl(url+"/tasks/list?type="+type+"&activityId="+activitys.getId());
                    break;
                case 2:
                    //签到活动
                    activitys.setUrl(url+"/cyuser/signlist?activityId="+activitys.getId());
                    break;
                case 3:
                    //助力活动
                    activitys.setUrl(url+"/cyuser/zhuli?activityId="+activitys.getId());
                    break;
                case 4:
                    activitys.setUrl(url+"/cyuser/xuan?activityId="+activitys.getId());
                    //宣传活动
                    break;
            }
        }


        return  AppResultData.success(pageInfo.getTotal()+"",activitysList1);
    }


    private  String getUrl(HttpServletRequest request){
        String url = "";
        url = request.getScheme() +"://" + request.getServerName()
                + ":" +request.getServerPort();
                //+ request.getServletPath();
        if(request.getServerPort()==80){
            url = request.getScheme() +"://" + request.getServerName();
                    //+ request.getServletPath();
        }
//        if (request.getQueryString() != null){
//            url += "?" + request.getQueryString();
//        }
        return  url;
    }

    public  AppResultData saveOrUpdate(Activitys activitys){
        if(activitys.getId()==null||(activitys.getId()+"").equals("0")){
            activitysMapper.insert(activitys);
        }else {
           // activitysMapper.insert(activitys);
            activitysMapper.updateByPrimaryKeySelective(activitys);
        }
        return  AppResultData.successMessage("成功");
    }

    public  AppResultData del(Integer id){
        Activitys activitys=activitysMapper.selectByPrimaryKey(id);
        activitys.setDelflag(activitys.getDelflag()==0?1:0);
        activitysMapper.updateByPrimaryKeySelective(activitys);
        return  AppResultData.successMessage("成功");
    }

    public   Activitys selectByPrimaryKey(Integer id){
        return  activitysMapper.selectByPrimaryKey(id);
    }

    public  int updateByPrimaryKeySelective(Activitys record){
        return  activitysMapper.updateByPrimaryKeySelective(record);
    }
}
